package cn.life.business.role.controller;

import cn.life.business.role.entity.po.RolePo;
import cn.life.business.role.service.RoleService;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description:    用户角色Controller
 *
 * @Author 李佳明
 * @Date 2018-07-25 08:53
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("add")
    public CMessage addRole(@RequestBody RolePo rolePo, @MyUser MyUserInfo myUserInfo){
        setRoleOprData(rolePo,myUserInfo);
        return roleService.addRole(rolePo);
    }

    @PostMapping("update")
    public CMessage updateRole(@RequestBody RolePo rolePo, @MyUser MyUserInfo myUserInfo){
        return roleService.updateRole(rolePo);
    }

    @PostMapping("delete")
    public CMessage deleteRole(@RequestBody RolePo rolePo, @MyUser MyUserInfo myUserInfo){
        return roleService.deleteRole(rolePo);
    }

    @PostMapping("queryList")
    public CMessage queryRoleList(@RequestBody RolePo rolePo, @MyUser MyUserInfo myUserInfo){
        if(rolePo.getProjectId() == null){
            rolePo.setProjectId(myUserInfo.getProjectId());
        }
        return roleService.queryRoleList(rolePo);
    }

    @PostMapping("queryDetail")
    public CMessage queryRoleDetail(@RequestBody RolePo rolePo, @MyUser MyUserInfo myUserInfo){
        return roleService.queryRoleDetail(rolePo);
    }

    public void setRoleOprData(RolePo rolePo,MyUserInfo myUserInfo){
        rolePo.setOprUserId(myUserInfo.getUserId());
        rolePo.setOprUserName(myUserInfo.getUserName());
        rolePo.setOprDate(new Date());
    }
}
